import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

public class _0314_有效括号 {

    static Map<Character,Character> map = new HashMap<>();
    static LinkedList<Character> stack = new LinkedList<>();
    static {
        map.put('(',')');
        map.put('[',']');
        map.put('{','}');
    }

    public static boolean isValid(String s) {
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char a = chars[i];
            if (map.containsKey(a)) {
                stack.push(a);
            }else {
                if (stack.isEmpty()) {
                    return false;
                }
                char character = map.get(stack.pop());
                if (character != a) {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
    public static void main(String[] args) {
        System.out.println(isValid("(]"));
    }

}
